<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_kampus extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.kd_kampus';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.kd_kampus, b.Nama AS NamaUniversitas, a.Nama AS NamaKampus, a.Alamat, a.Kota, a.Telepon, a.Fax, a.status');
			$this->db->from('ref_kampus a');
			$this->db->join('ref_universitas b', 'b.kd_pt = a.kd_pt');
			$this->db->where('a.status', "A");
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('a.kd_kampus, a.Nama AS NamaKampus, b.Nama AS NamaUniv, a.Alamat, a.Kota, a.Telepon, a.Fax, a.status, a.kd_pt');
		$this->db->from('ref_kampus a');
		$this->db->join('ref_universitas b', 'b.kd_pt = a.kd_pt','left');
		$this->db->where('a.kd_kampus', $kode);
		
		return $this->db->get();
	}
	
	
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}
		return json_encode($response);
	}
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('kd_kampus',$kode);
		
		$this->db->select('*');
		$this->db->from('ref_kampus');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('kd_kampus', $data['kd_kampus']);
		$this->db->set('Nama', $data['Nama']);
		$this->db->set('Alamat', $data['Alamat']);
		$this->db->set('Kota', $data['Kota']);
		$this->db->set('kd_pt', $data['kd_pt']);
		$this->db->set('Telepon', $data['Telepon']);
		$this->db->set('Fax', $data['Fax']);
		$this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->set('CUID', $this->session->userdata('user_id'));
        $this->db->set('CDATE', date('Y-m-d H:i:s'));
		
		$result = $this->db->insert('ref_kampus');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function UpdateOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('kd_kampus', $data['kd_kampus']);
		$this->db->set('Nama', $data['Nama']);
		$this->db->set('Alamat', $data['Alamat']);
		$this->db->set('Kota', $data['Kota']);
		$this->db->set('kd_pt', $data['kd_pt']);
		$this->db->set('Telepon', $data['Telepon']);
		$this->db->set('Fax', $data['Fax']);
		$this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('kd_kampus', $data['kd_kampus']);
		$result = $this->db->update('ref_kampus');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function DeleteOnDb($kode){
		//query delet data  dengan ganti status db            
		$this->db->flush_cache();
		
		$this->db->set('status', "T");
	   
		$this->db->set('MUID', $this->session->userdata('user_id'));
		$this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('kd_kampus', $kode);
		$result = $this->db->update('ref_kampus');
	   
	   
		//return
		if($result) {
				return TRUE;
		}else {
				return FALSE;
		}
	}
	
	/*function get_pdf(){
		# get data
		$this->db->flush_cache();
		$this->db->select('*, b.Nama AS nama_universitas, a.Nama AS nama_kampus, a.Kota, a.Fax, a.Telepon, a.Kota', false);
		$this->db->from('ref_kampus a');
		$this->db->join('ref_universitas b', 'b.kd_pt = a.kd_pt');

		// proses
		$result = $this->db->get();
		
		# init column
		$pdfdata['column'] = array();
		array_push($pdfdata['column'], array('caption'=>'No.', 'width'=>10, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama Universitas', 'width'=>70, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama Kampus', 'width'=>40, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Alamat', 'width'=>65, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Kota', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Telepon', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Fax', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		
		# init record
		$i=1;
		$pdfdata['row'] = array();
		foreach($result->result() as $row){
			array_push($pdfdata['row'], array(
				$i,
				$row->nama_universitas,
				$row->nama_kampus,
				$row->Alamat,
				$row->Kota,
				$row->Telepon,
				$row->Fax
			));
			$i++;
		}
		
		return $pdfdata;
		
	}*/
	
	//PDF
	function get_pdf(){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
		$this->db->flush_cache();
		$this->db->select('*, a.Nama AS nama_kampus, a.Kota, a.Fax, a.Telepon, a.Kota', false);
		$this->db->from('ref_kampus a');
			
		$this->db->where('a.status', 'A');

		// proses
		$result = $this->db->get();
		
		if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
	}
	
}

?>